package com.mypage.view.activity.picutils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.ChatUser;
import com.cloudcc.mobile.im.video.util.Utils;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.view.im.ConversationListActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 瀵瑰浘鐗囪繘琛岀紪杈戠殑activity
 *
 * @author yyw
 *
 */
public class DrawAct extends Activity implements Callback, OnClickListener,
		OnCheckedChangeListener, OnSeekBarChangeListener,
		PlatformActionListener {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	public static final String PIC_ACTION = "action";
	public static final String PIC_PATH = "file_uri";
	private static final String TAG = "MainActivity";
	private Button btnCancle, btnSava, btnBack;
	private RadioGroup group, colorGroup;
	private CameraSurfaceView surfaceView;
	private File files;
	private Bitmap bitmap;
	private Button btnOne;
	private Button btnTwo;
	private Button btnThree;
	private LinearLayout layoutLine;
	private String judge;
	private String mImgname;
	private String mPname;

	public String mEntityId;
	public String mEntityNameID;
	public String mJiLuName;
	public String data;
	public String Objame;
	public Bundle bundle ;
	public Intent intent;

	/**
	 *
	 * @param fromFilePath
	 * @param toFilePath
	 * @return
	 */
	public static Boolean copyFile(String fromFilePath, String toFilePath) {
		FileInputStream fis = null;
		FileOutputStream fos = null;

		try {
			fis = new FileInputStream(fromFilePath);
			fos = new FileOutputStream(toFilePath);

			byte[] buff = new byte[1024];
			while (fis.read(buff) > 0) {
				fos.write(buff);
			}

			fis.close();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {

		}
		return true;
	}

	/**
	 *
	 *
	 * @param uri
	 * @return
	 */
	private String getPath(Uri uri) {
		String[] projection = { Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(Images.Media.DATA);
		cursor.moveToFirst();
		String path = cursor.getString(column_index);
		cursor.close();
		return path;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.draw_picture);
		 intent = getIntent();
		 bundle = intent.getExtras();
		judge = bundle.getString("share");
		mPname = bundle.getString("pName");
		mEntityNameID = bundle.getString("mEntityNameID");
		mEntityId = bundle.getString("mEntityId");
		mJiLuName = bundle.getString("mJiLuName");
		Objame = bundle.getString("Objame");
		Log.i(TAG, "onCreate");
		btnCancle = (Button) findViewById(R.id.cancle);
		group = (RadioGroup) findViewById(R.id.group);
		layoutLine = (LinearLayout) findViewById(R.id.layoutLine);
		btnSava = (Button) findViewById(R.id.sava);

		btnOne = (Button) findViewById(R.id.btnOne);
		btnTwo = (Button) findViewById(R.id.btnTwo);
		btnThree = (Button) findViewById(R.id.btnThree);

		colorGroup = (RadioGroup) findViewById(R.id.color);
		surfaceView = (CameraSurfaceView) findViewById(R.id.sv);
		btnBack = (Button) findViewById(R.id.back);
		surfaceView.getHolder().addCallback(this);

		btnOne.setOnClickListener(this);
		btnTwo.setOnClickListener(this);
		btnThree.setOnClickListener(this);

		btnCancle.setOnClickListener(this);
		btnSava.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		group.setOnCheckedChangeListener(this);
		colorGroup.setOnCheckedChangeListener(this);
		getCamera();
		selectColor(colorGroup.getCheckedRadioButtonId());
	}

	/**
	 * 鎵撳紑鐩稿唽鎴栬�呯浉鏈�
	 */
	private void getCamera() {
		// Intent data = getIntent();
		// String action = data.getStringExtra(PIC_ACTION);
		// String file = data.getStringExtra(PIC_PATH);
		String file = Environment.getExternalStorageDirectory()
				+ "/DCIM/Camera/" + "lyj.jpg";
		files = new File(file);
		// if (MediaStore.ACTION_IMAGE_CAPTURE.equals(action)) {
		// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		// intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(files));
		// // startActivityForResult(intent, 2234);
		// startActivity(intent);
		// }
	}

	public void onClick(View v) {
		Log.i(TAG, "onClick");
		switch (v.getId()) {
		case R.id.sava:
			Bitmap bitmap = surfaceView.getmBitmap();
			if (bitmap != null) {
				// savaToSd(bitmap);
				mImgname = UUID.randomUUID().toString();
				saveMyBitmap(mImgname, bitmap);
			}
			doShare();
			finish();
			// finish();
			break;
		case R.id.cancle:
			surfaceView.back();
			break;
		case R.id.back:
			finish();
			break;
		// 添加按鈕
		case R.id.btnOne:
			surfaceView.setStrokeWidths(5);
			btnOne.setBackgroundResource(R.drawable.draw_oval_blue);
			btnTwo.setBackgroundResource(R.drawable.draw_oval_black);
			btnThree.setBackgroundResource(R.drawable.draw_oval_black);

			break;
		case R.id.btnTwo:
			surfaceView.setStrokeWidths(10);
			btnOne.setBackgroundResource(R.drawable.draw_oval_black);
			btnTwo.setBackgroundResource(R.drawable.draw_oval_blue);
			btnThree.setBackgroundResource(R.drawable.draw_oval_black);
			break;
		case R.id.btnThree:
			surfaceView.setStrokeWidths(15);
			btnOne.setBackgroundResource(R.drawable.draw_oval_black);
			btnTwo.setBackgroundResource(R.drawable.draw_oval_black);
			btnThree.setBackgroundResource(R.drawable.draw_oval_blue);
			break;
		default:
			break;
		}
	}

	/**
	 * 保存到SD卡
	 *
	 * @param bitmap
	 */
	private void savaToSd(Bitmap bitmap) {
		if (files == null) {
			return;
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//		FileOutputStream fos = null;
		try {
		FileOutputStream fos = new FileOutputStream(files);
			fos.write(baos.toByteArray());
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		surfaceView.setCanDraw(true);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		Log.i(TAG, "surfaceChanged");
		if (bitmap == null) {
			String str = files.getAbsolutePath();
			bitmap = getZoomBmpByDecodePath(files.getAbsolutePath(), width,
					height).copy(Bitmap.Config.ARGB_8888, true);
			// getBitmao();
			LayoutParams layoutParams = surfaceView.getLayoutParams();
			layoutParams.height = bitmap.getHeight();
			layoutParams.width = bitmap.getWidth();
			surfaceView.setLayoutParams(layoutParams);
			Log.i(TAG,
					"bitmap: " + bitmap.getWidth() + "-" + bitmap.getHeight());
			Log.i(TAG, "sv: " + width + "-" + height);
			surfaceView.setmBitmap(bitmap);
			surfaceView.setCanDraw(true);
		}
	}

	public Bitmap getZoomBmpByDecodePath(String path, int w, int h) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);
		options.inSampleSize = calculateInSampleSize(options, w, h);
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(path, options);
	}

	/**
	 * 璁＄畻缂╃暐鍥惧帇缂╃殑姣斿垪锛屽洜涓烘瘡寮犲浘鐗囬暱瀹戒笉涓�鏍凤紝鍘嬬缉姣斿垪涔熶笉涓�鏍�
	 *
	 * @param options
	 * @param reqWidth
	 * @param reqHeight
	 * @return
	 */
	public int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;
		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) reqHeight);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}
		}
		return inSampleSize;
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (group.equals(this.group)) {
			selectPath(checkedId);
		} else if (group.equals(colorGroup)) {
			selectColor(checkedId);
		}

	}

	/**
	 * 閫夋嫨鍥惧舰
	 *
	 * @param checkedId
	 */
	private void selectPath(int checkedId) {
		RadioButton mNothing = (RadioButton) group.findViewById(R.id.nothing);
		switch (checkedId) {

		case R.id.word:
			layoutLine.setVisibility(View.GONE);

			colorGroup.setVisibility(View.GONE);

			btnCancle.setVisibility(View.VISIBLE);

			mNothing.setVisibility(View.VISIBLE);
			surfaceView.setCurrentDraw(CameraSurfaceView.DRAW_WORD);
			break;
		case R.id.line:
			layoutLine.setVisibility(View.VISIBLE);

			colorGroup.setVisibility(View.VISIBLE);

			btnCancle.setVisibility(View.VISIBLE);

			mNothing.setVisibility(View.VISIBLE);
			surfaceView.setCurrentDraw(CameraSurfaceView.DRAW_PATH);
			break;
		case R.id.rect:
			surfaceView.setCurrentDraw(CameraSurfaceView.DRAW_RECT);
			break;
		case R.id.circle:
			surfaceView.setCurrentDraw(CameraSurfaceView.DRAW_CIRCLE);
			break;
		case R.id.arrow:
			surfaceView.setCurrentDraw(CameraSurfaceView.DRAW_ARROW);
			break;
		case R.id.oval:
			surfaceView.setCurrentDraw(CameraSurfaceView.DRAW_OVAL);
			break;
		case R.id.nothing:
			surfaceView.setCurrentDraw(CameraSurfaceView.DRAW_NOTHING);
			break;
		default:
			break;
		}
	}

	/**
	 * 閫夋嫨鐢荤瑪棰滆壊
	 *
	 * @param checkedId
	 */
	private void selectColor(int checkedId) {
		switch (checkedId) {
		case R.id.green:
			surfaceView.setCurrentDraw(CameraSurfaceView.DRAW_PATH);
			surfaceView.setColor(Color.GREEN);
			changeOtherColor(Color.GREEN);
			break;
		case R.id.blue:
			surfaceView.setColor(Color.BLUE);
			changeOtherColor(Color.BLUE);
			break;
		case R.id.yellow:
			surfaceView.setCurrentDraw(CameraSurfaceView.DRAW_PATH);
			surfaceView.setColor(Color.YELLOW);
			changeOtherColor(Color.YELLOW);
			break;
		case R.id.red:
			surfaceView.setCurrentDraw(CameraSurfaceView.DRAW_PATH);
			surfaceView.setColor(Color.RED);
			changeOtherColor(Color.RED);
			break;
		case R.id.black:
			surfaceView.setColor(Color.BLACK);
			changeOtherColor(Color.BLACK);
			break;
		case R.id.white:
			surfaceView.setColor(Color.WHITE);
			changeOtherColor(Color.WHITE);
			break;
		default:
			changeOtherColor(Color.BLACK);
			break;
		}
	}

	private void changeOtherColor(int color) {
		for (int i = 0; i < colorGroup.getChildCount(); i++) {
			if (colorGroup.getChildAt(i) instanceof TextView) {
				TextView tvChild = (TextView) colorGroup.getChildAt(i);
				tvChild.setTextColor(color);
			}
		}
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		if (fromUser) {// 閫夋嫨瀛椾綋澶у皬
			surfaceView.setTextSize(progress + 80);

		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {

	}

	@Override
	protected void onDestroy() {
		if (bitmap != null) {
			bitmap.recycle();
			bitmap = null;
		}
		super.onDestroy();
	}

	public Bitmap getBitmao() {
		String path = Environment.getExternalStorageDirectory()
				+ "/DCIM/Camera/" + "lyj.jpg";
		File mFile = new File(path);
		// 若该文件存在
		if (mFile.exists()) {
			Bitmap bitmap = BitmapFactory.decodeFile(path);
			return bitmap;
		}
		return null;
	}

	public void doShare() {

		if (judge.equals("qq")) {
			setShareNetwork();
			QQ();
		} else if (judge.equals("zone")) {
			setShareNetwork();
			QZone();
		} else if (judge.equals("wechat")) {
			setShareNetwork();
			WeChat();
		} else if (judge.equals("pengyouquan")) {
			setShareNetwork();
			WeCircle();
		} else if (judge.equals("yunxin")) {
			YunXin();
		} else {// sina
			setShareNetwork();
			SinaWeibo();
		}
	}

	public void QQ() {
		ShareParams QQ = new ShareParams();
		// QQ.setTitle("我是分享标题");
		// 点击标题跳转的分享网址
		// QQ.setTitleUrl("http://sharesdk.cn");

		// QQ.setText("我是分享内容");
		// qq.setUrl("http://mob.com"); //空间没有这个
		// QQ.setImageUrl("http://cdn6.jinxidao.com/uploads/201607/5791d21f3f84b.jpg");
		QQ.setImagePath("/sdcard/" + mImgname + ".png");
		// site是分享此内容的网站名称，仅在QQ空间使用
		// QQ.setSite("分享到QQ空间");
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		// QQ.setSiteUrl("http://sharesdk.cn");
		Platform QQS = ShareSDK.getPlatform(DrawAct.this,
				cn.sharesdk.tencent.qq.QQ.NAME);
		// 设置监听回调
		QQS.setPlatformActionListener(DrawAct.this);
		QQS.share(QQ);
	}

	public void QZone() {
		ShareParams qq = new ShareParams();
		// qq.setTitle("我是分享标题");
		// 点击标题跳转的分享网址
		// qq.setTitleUrl("http://sharesdk.cn");

		// qq.setText("我是分享内容");
		// qq.setUrl("http://mob.com"); //空间没有这个
		// qq.setImageUrl("http://cdn6.jinxidao.com/uploads/201607/5791d21f3f84b.jpg");

		// site是分享此内容的网站名称，仅在QQ空间使用
		// qq.setSite("分享到QQ空间");
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		// qq.setSiteUrl("http://sharesdk.cn");
		qq.setImagePath("/sdcard/" + mImgname + ".png");
		// q.setImagePath("/storage/emulated/0/cloud.png");
		Platform share_qq = ShareSDK.getPlatform(DrawAct.this, QZone.NAME);
		// 设置监听回调
		share_qq.setPlatformActionListener(DrawAct.this);
		share_qq.share(qq);
	}

	public void WeChat() {
		ShareParams wechat = new ShareParams();
		// wechat.setTitle("我是分享标题");
		// wechat.setText("我是分享文本内容");
		// wechat.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg ");
		// wechat.setUrl("http://mob.com ");
		// wechat.setShareType(Platform.SHARE_WEBPAGE);
		// wechat.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
		wechat.setShareType(Platform.SHARE_IMAGE);
		wechat.setImagePath("/sdcard/" + mImgname + ".png");
		Platform weixin = ShareSDK.getPlatform(DrawAct.this, Wechat.NAME);
		if (weixin.isClientValid()) {
			System.out.println("安装了微信");
		} else {
			System.out.println("没有安装了微信");
		}

		weixin.setPlatformActionListener(DrawAct.this);
		weixin.share(wechat);
	}

	public void WeCircle() {
		ShareParams wechatMoments = new ShareParams();
		// wechatMoments.setTitle("我是分享标题");
		// wechatMoments.setText("我是分享文本内容");
		// wechatMoments
		// .setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg ");
		// wechatMoments.setUrl("http://mob.com ");
		wechatMoments.setShareType(Platform.SHARE_IMAGE);
		wechatMoments.setImagePath("/sdcard/" + mImgname + ".png");
		Platform weixinMoments = ShareSDK.getPlatform(DrawAct.this,
				WechatMoments.NAME);
		if (weixinMoments.isClientValid()) {
			System.out.println("安装了微信");
		} else {
			System.out.println("没有安装了微信");
		}

		weixinMoments.setPlatformActionListener(DrawAct.this);
		weixinMoments.share(wechatMoments);
	}

	public void SinaWeibo() {
		ShareParams sina = new ShareParams();
		// sina.setTitle("我是分享标题");
		// // 点击标题跳转的分享网址
		// sina.setTitleUrl("http://sharesdk.cn");
		//
		// sina.setText("我是分享内容");
		// // qq.setUrl("http://mob.com"); //空间没有这个
		// sina.setImageUrl("http://cdn6.jinxidao.com/uploads/201607/5791d21f3f84b.jpg");
		//
		// // site是分享此内容的网站名称，仅在QQ空间使用
		// sina.setSite("分享到QQ空间");
		// // siteUrl是分享此内容的网站地址，仅在QQ空间使用
		// sina.setSiteUrl("http://sharesdk.cn");
		sina.setImagePath("/sdcard/" + mImgname + ".png");
		Platform sinaS = ShareSDK.getPlatform(DrawAct.this, SinaWeibo.NAME);
		// 设置监听回调
		sinaS.setPlatformActionListener(DrawAct.this);
		sinaS.share(sina);
	}

	@Override
	public void onCancel(Platform arg0, int arg1) {
		// TODO Auto-generated method stub
		if ("en".equals(mEns)) {
			Toast.makeText(DrawAct.this, "Cancel sharing", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(DrawAct.this, "取消分享", Toast.LENGTH_SHORT).show();
	}


	}
	public void setShareNetwork(){

		// TODO Auto-generated method stub
		if("en".equals(mEns)){
			Toast.makeText(DrawAct.this, "sharing success", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(DrawAct.this, "分享成功", Toast.LENGTH_SHORT).show();
		}

		String mStrlyj = bundle.getString("Objame");
		String mJilu = bundle.getString("mJiLuName");
		if (judge.equals("qq")) {
			createJson("分享 "+mStrlyj +" " +"("+mJilu +")"+" "+"到QQ");
			setShareHttp();
		} else if (judge.equals("zone")) {
			createJson("分享 "+mStrlyj+" "+"("+mJilu +")"+" "+"到QQ空间");
			setShareHttp();
		} else if (judge.equals("wechat")) {
			createJson("分享 "+mStrlyj+" "+"("+mJilu +")"+" "+"到微信");
			setShareHttp();
		} else if (judge.equals("pengyouquan")) {
			createJson("分享 "+mStrlyj+ " "+"("+mJilu +")"+" "+"到朋友圈");
			setShareHttp();
		}  else {
			createJson("分享 "+mStrlyj+ " "+"("+mJilu +")"+" "+"到新浪微博");
			setShareHttp();
//			SinaWeibo();
		}

	}
	@Override
	public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
		// TODO Auto-generated method stub
		if ("en".equals(mEns)) {
			Toast.makeText(DrawAct.this, "Share success", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(DrawAct.this, "分享成功", Toast.LENGTH_SHORT).show();
	}

//		String mStrlyj = bundle.getString("Objame");
//		String mJilu = bundle.getString("mJiLuName");
//		if (judge.equals("qq")) {
//			createJson("分享 "+mStrlyj +" " +"("+mJilu +")"+" "+"到QQ");
//			setShareHttp();
//		} else if (judge.equals("zone")) {
//			createJson("分享 "+mStrlyj+" "+"("+mJilu +")"+" "+"到QQ空间");
//			setShareHttp();
//		} else if (judge.equals("wechat")) {
//			createJson("分享 "+mStrlyj+" "+"("+mJilu +")"+" "+"到微信");
//			setShareHttp();
//		} else if (judge.equals("pengyouquan")) {
//			createJson("分享 "+mStrlyj+ " "+"("+mJilu +")"+" "+"到朋友圈");
//			setShareHttp();
//		}  else {
//			createJson("分享 "+mStrlyj+ " "+"("+mJilu +")"+" "+"到新浪微博");
//			setShareHttp();
////			SinaWeibo();
//		}
//

	}

	@Override
	public void onError(Platform arg0, int arg1, Throwable arg2) {
		// TODO Auto-generated method stub
		if("en".equals(mEns)){
			Toast.makeText(DrawAct.this, "Share failure"+arg2, Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(DrawAct.this, "分享失败"+arg2, Toast.LENGTH_SHORT).show();
		}

		int ss = arg1;
		int s = ss;
	}

	/**
	 *
	 * @param bitName保存图片到sd卡
	 * @param mBitmap
	 */
	public void saveMyBitmap(String bitName, Bitmap mBitmap) {
		File f = new File("/sdcard/" + bitName + ".png");
		try {
			f.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// DebugMessage.put("在保存图片时出错：" + e.toString());
		}
		FileOutputStream fOut = null;
		try {
			fOut = new FileOutputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
		try {
			fOut.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fOut.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 云信 auter liuyongjie
	 */
	public void YunXin() {
		String path = Environment.getExternalStorageDirectory().getPath() + "/"
				+ mImgname + ".png";
		SharedPreferences pref = this.getSharedPreferences("data", 0);
		SharedPreferences.Editor editor = pref.edit();
		editor.clear();
		editor.putString("lyj", path);
		editor.putString("lyjs", mPname);
		editor.putString("mmEntityId",mEntityId);
		editor.putString("mmEntityNameID",mEntityNameID);
		editor.putString("mmJiLuName", mJiLuName);
		editor.putString("Objame", Objame);
		editor.putString("moperationId", RunTimeManager.getInstance().getUserId());
		editor.commit();
		Intent i = new Intent();
		i.setClass(this, ConversationListActivity.class);
		i.putExtra("url", "正在分享");
		startActivity(i);
		// setParseUri();
	}

	// 转成uri
	public void setParseUri() {
		String mUri = null;
		String path = Environment.getExternalStorageDirectory().getPath()
				+ "/cloud.png";
		Log.d(TAG, "path1 is " + path);
		if (path != null) {
			path = Uri.decode(path);
			Log.d(TAG, "path2 is " + path);
			ContentResolver cr = this.getContentResolver();
			StringBuffer buff = new StringBuffer();
			buff.append("(").append(Images.ImageColumns.DATA).append("=")
					.append("'" + path + "'").append(")");
			Cursor cur = cr.query(Images.Media.EXTERNAL_CONTENT_URI,
					new String[] { Images.ImageColumns._ID }, buff.toString(),
					null, null);
			int index = 0;
			for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
				index = cur.getColumnIndex(Images.ImageColumns._ID);
				// set _id value
				index = cur.getInt(index);
			}
			if (index == 0) {
				// do nothing
			} else {
				Uri uri_temp = Uri
						.parse("content://media/external/images/media/" + index);
				Log.d(TAG, "uri_temp is " + uri_temp);
				if (uri_temp != null) {
					Uri uri = uri_temp;
					mUri = uri.toString();
					SharedPreferences pref = this.getSharedPreferences("data",
							0);
					SharedPreferences.Editor editor = pref.edit();
					editor.clear();
					editor.putString("lyj", mUri);
					editor.commit();
					Intent i = new Intent();
					i.setClass(this, ConversationListActivity.class);
					i.putExtra("url", mUri);
					startActivity(i);
				}
			}

		}
	}

	// 统计分享日志
	public void setShareHttp() {

		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("binding", RunTimeManager.getInstance().getServerBinding());
		params.addBodyParameter("objectApiName", mEntityNameID);
		params.addBodyParameter("data",data);
				params.addBodyParameter("serviceName","monitorLog");

		utils.send(HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						String result = arg0.result;
					}

				});

	}
	//把数据转换成json格式。用于上传服务端
	public void createJson(String str){
		 JSONObject object = new JSONObject();

	        try {
	        	object.put("recordId", mEntityId);
	        	object.put("recordName", mJiLuName);
	        	object.put("operationId", RunTimeManager.getInstance().getUserId());
	        	object.put("loginName", RunTimeManager.getInstance().getServerloginname());

	        	object.put("deletecause", str);
	        	object.put("deleteType", "fenxiangandroid");

	            data = object.toString();
	            System.out.println(object);
//	            Log.d("/////////////////", object.toString());
	        } catch (JSONException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	}
}
